package com.qiyu.wakaka.common.result;

import java.util.HashMap;

/**
 * @author daniu
 * @date 2018/10/18 20:33
 * @description:
 */
public class JsonResult extends HashMap<String,Object>{

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private int errorCode;
    /** 错误消息  */
    private String message;
    /** 返回正确时候的数据 */
    private Object data;

    public JsonResult() {
    }

    /**
     * 返回错误消息
     *
     * @return 错误消息
     */
    public static JsonResult error()
    {
        return error(1, "操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 内容
     * @return 错误消息
     */
    public static JsonResult error(String msg)
    {
        return error(500, msg);
    }

    /**
     * 返回错误消息
     *
     * @param errorCode 错误码
     * @param message 内容
     * @return 错误消息
     */
    public static JsonResult error(int errorCode, String message)
    {
        JsonResult json = new JsonResult();
        json.put("errorCode", errorCode);
        json.put("message", message);
        return json;
    }

    /**
     * 返回成功消息
     *
     * @param message 内容
     * @return 成功消息
     */
    public static JsonResult success(String message)
    {
        JsonResult json = new JsonResult();
        json.put("message", message);
        json.put("errorCode", 0);
        return json;
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static JsonResult success()
    {
        return JsonResult.success("操作成功");
    }

    @Override
    public JsonResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }

    public static JsonResult result(String msg){
        JsonResult json = new JsonResult();
        if("SUCCESS".equals(msg)){
            json.put("errorCode", SUCCESS);;
        }else {
            json.put("errorCode", ERROR);;
        }
        json.put("message", msg);
        return json;
    }

    public static JsonResult result(Object data){
        JsonResult json = new JsonResult();
        json.put("message", "SUCCESS");
        json.put("errorCode", 0);
        json.put("data",data);
        return json;
    }

    public JsonResult(Throwable e) {
        errorCode = ERROR;
        message = e.getMessage();
    }

    public JsonResult(int state, Throwable e) {
        this.errorCode = state;
        this.message = e.getMessage();
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int state) {
        this.errorCode = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "JsonResult [errorCode=" + errorCode + ", message=" + message + ", data=" + data + "]";
    }

}
